//
//  Album.m
//  UIHomeWork05-相册轮播图
//
//  Created by tusm on 16/7/21.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "Album.h"

@implementation Album
{
    CGFloat _offSet;
    NSInteger _value;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame images:(NSArray<UIImage *> *)images {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createScrollView:images];
    }
    return self;
}


- (void)createScrollView:(NSArray *)images {
    
    _value = images.count;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:_scrollView];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
//    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.scrollView.contentSize = CGSizeMake((images.count + 2) * WIDTH, 0);
    ScrollerView *firstScrollView = [[ScrollerView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) image:images.lastObject];
    [self.scrollView addSubview:firstScrollView];
    
    for (int i = 0; i < images.count; i++) {
        
        ScrollerView *scroll = [[ScrollerView alloc] initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT) image:images[i]];
        [self.scrollView addSubview:scroll];
    }
    
    ScrollerView *lastScrollView = [[ScrollerView alloc] initWithFrame:CGRectMake(WIDTH * (images.count + 1), 0, WIDTH, HEIGHT) image:images.firstObject];
    [self.scrollView addSubview:lastScrollView];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - 50, WIDTH, 50)];
    [self addSubview:_page];
    self.page.numberOfPages = images.count;
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)pageAction:(UIPageControl *)page {
    
    [self.scrollView setContentOffset:CGPointMake(WIDTH * page.currentPage, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == WIDTH * (_value + 1)) {
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    } else if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake(WIDTH * _value, 0);
    }
    
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    
    for (UIView *view in self.scrollView.subviews) {
        
        if ([view isKindOfClass:[ScrollerView class]]) {
            
            if (scrollView.contentOffset.x != _offSet) {
                
                ScrollerView *scroll = (ScrollerView*)view;
                scroll.zoomScale = 1;
            }
        }
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    _offSet = self.scrollView.contentOffset.x;
}

@end
